<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>活码页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
            margin-top: 50%;
            transform: translateY(-25%);
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
        }

        .container {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 90vw;
            height: 100%;
            text-align: center;
        }

        @media (min-width: 1024px) {
            .container {
                background: #fff;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                width: 400px;
                height: 100%;
                text-align: center;
            }
        }

        .top-text {
            background: #333;
            color: #fff;
            font-size: 33px;
            padding: 20px 0;
            margin-bottom: 20px;
        }

        .avatar {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            margin-right: 10px;
            vertical-align: middle;
        }

        .sub-title {
            font-size: 33px;
            display: inline-block;
            vertical-align: middle;
        }

        .scan-count {
            font-size: 30px;
            color: #666;
            margin-top: 5px;
        }

        .qrcode-container {
            margin: 20px auto;
            border: 2px dashed #ddd;
            padding: 20px;
            width: 100%;
            text-align: center;
        }

        .qrcode {
            width: 95%;
        }

        .bottom-text {
            font-size: 33px;
            background: #333;
            color: #fff;
            padding: 20px 0;
            margin-top: 20px;
        }

        .long-press {
            font-size: 33px;
            color: #666;
            margin-top: 10px;
        }

        .sub-title-container {
            display: flex;
            align-items: center;
            padding-left: 60px;
        }
    </style>
</head>
<body>
<div id="app">
    <div>
        <div class="container" v-if="look">
            <div class="top-text" v-if="code_obj.type === '1' ">{{code_obj.topText}}</div>
            <!-- 在容器内添加隐藏的 ID 字段 -->
            <div class="sub-title-container" v-if="code_obj.type === '1' ">
                <img :src="code_obj.avatar" alt="Avatar" class="avatar">
                <div style="flex: 1;text-align: left;">
                    <div class="sub-title">{{code_obj.title}}</div>
                    <!-- <div class="scan-count">已有{{code_obj.code_urls[index].already_frequency}}人扫码</div> -->
                </div>
            </div>
            <div class="qrcode-container">
                <a :href="code_obj.code_urls[index].code_url" target="_blank" id="qrLink">
                    <img :src="code_obj.code_urls[index].code_url" alt="QR Code" class="qrcode" id="qrImage">
                </a>
            </div>
            <div v-if="code_obj.type === '1' " class="long-press" id="codeTitle">{{code_obj.code_urls[index].code_title}}</div>
            <div v-if="code_obj.type === '1' " class="long-press" style="margin-top: 50px;margin-bottom: 50px;display: none" id="switchButton" @click="next">点击此处切换下一个</div>
            <div class="long-press">长按识别上方二维码</div>
            <div class="bottom-text">{{code_obj.bottomText}}</div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script>
    new Vue({
        el: '#app',
        data:{
            code_obj:{},
            index:0,
            longPressTimer: null,
            isRequesting: false,
            look: false,
        },
        mounted(){
            this.init()
        },
        methods:{
            handleLongPressStart: function () {
                if (this.isRequesting) return; // 防止重复请求
                this.longPressTimer = setTimeout(() => {
                    this.sendAlreadyRequest();
                }, 500); // 长按 500ms 后触发
            },
            handleLongPressEnd: function () {
                clearTimeout(this.longPressTimer);
                this.longPressTimer = null;
            },
            sendAlreadyRequest:  async function () {
                if (this.isRequesting) return;
                this.isRequesting = true;

                try {
                    const response = await fetch('https://code.server.wangyinjie.cn/api/admin/livecode/already', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            id:  this.code_obj.id,
                            url: this.code_obj.code_urls[this.index].url
                        })
                    });

                    if (!response.ok) {
                        throw new Error(`请求失败: ${response.statusText}`);
                    }
                } finally {
                    this.isRequesting = false;
                }
            },
           init: function (){
                const urlParams = new URLSearchParams(window.location.search);
                const id = urlParams.get('id');
                if (id) {
                    fetch(`https://code.server.wangyinjie.cn/code/${id}`, {
                        method: 'GET',
                    })
                        .then(response => response.json())
                        .then(data => {
                        console.log(data);
                        this.code_obj = data;
                        this.look = true;
                        document.title = this.code_obj.title; // 修改页面标题
                        this.bindQrImageEvents();
                    }).catch(error => {
                        console.error('请求失败:', error);
                    });
                } else {
                    console.warn('未找到 id 参数');
                }
            },
            bindQrImageEvents:function() {
                this.$nextTick(() => {
                    const qrImage = document.getElementById('qrImage');
                    if (qrImage) {
                        qrImage.addEventListener('touchstart', this.handleLongPressStart);
                        qrImage.addEventListener('mousedown', this.handleLongPressStart);
                        qrImage.addEventListener('touchend', this.handleLongPressEnd);
                        qrImage.addEventListener('mouseup', this.handleLongPressEnd);
                        qrImage.addEventListener('mouseleave', this.handleLongPressEnd);
                    } else {
                        console.warn('二维码图片未渲染完成或不存在');
                    }
                });
            },
            next:function () {
                if(this.index===this.code_obj.code_urls.length){
                    this.index = 0
                }else{
                    this.index++
                }
            }
        }
    });
</script>
</body>
</html>